package com.ybkj.daijia.server.event.listener.errand;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.errand.Order;
import com.ybkj.daijia.server.event.model.errand.ErrandOrderConfirmEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.MemberService;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ErrandOrderConfirmEventListener implements
    ApplicationListener<ErrandOrderConfirmEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ErrandOrderConfirmEventListener.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private MemberService memberService;


    @Override
    @Async
    public void onApplicationEvent(ErrandOrderConfirmEvent event) {

        logger.debug("order Confirm event:{}", "");

        Order order = (Order) event.getSource();

        Passenger passenger = memberService.findOnePassenger(order.getPassengerId());
        //给客户推送
        passenger.setCid(memberService.findCid(passenger.getId()));
        if (StringUtils.isNotBlank(passenger.getCid())) {
            try {
                //发送设备推送
                PushData pushData = new PushData();
                pushData.setType("ORDER_CONFIRM_ERRAND");
                pushData.setTitle("新提醒");
                pushData.setDescription("服务人员已确认您的订单，请及时付款");
                pushData.setData(order.getId());

                GeTuiMember.push(passenger, pushData, settingUtils.get());
            } catch (IOException e) {
                logger.debug("push error : " + e.getMessage());
            }
        }

    }

}
